package cc.booku.owlbox.HomeDirectory;

import android.graphics.Color;
import android.os.Environment;
import android.os.Handler;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.R;

public class FileEditActivity extends AppCompatActivity {

    private Toolbar Toolbar;
    private EditText Ev_file;
    private TextView Fromfileurl;
    private String filePath = Environment.getExternalStorageDirectory() + File.separator + "OwlDatabase" + File.separator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_edit);

        //加载标题栏
        LoadToolbar();
        //状态栏颜色
        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));

        Ev_file = (EditText) findViewById(R.id.ev_file);
        Fromfileurl = (TextView) findViewById(R.id.fromfileurl);
        Fromfileurl.setText(filePath);
        RXDfun();
    }

    private void LoadToolbar() {
        //新页面接收数据


        Toolbar = (Toolbar) findViewById(R.id.toolbar);

        Toolbar.setTitle("文本编辑");

        setSupportActionBar(Toolbar);//利用Toolbar代替ActionBar
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到用户设置页面
                finish();
            }
        });
        //添加menu 菜单点击事件
        Toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_save:
                        //保存
                        //重新写入数据到文件
                        if (FileoOperations.fileIsExists(Fromfileurl.getText().toString())) {
                            //先输入空字符串，清空文件
                            //FileoOperations.writeAssignTxt(Fromfileurl.getText().toString(), "");
                            // Log.d("Filedetails", "Filedetails.toString()" + Filedetails.getText().toString());
                            //写入修改后的字符
                            FileoOperations.writeAssignTxt(Fromfileurl.getText().toString(), Ev_file.getText().toString().trim());

                            //等待刷新
                            WaitingRefresh();
                        } else
                            Toast.makeText(FileEditActivity.this, "编辑失败", Toast.LENGTH_SHORT).show();
                        break;

                }
                return true;
            }
        });
    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        getMenuInflater().inflate(R.menu.menu_save, menu);//toolbar添加menu菜单
        return true;
    }

    //等待刷新函数
    private void WaitingRefresh() {
        Toast.makeText(FileEditActivity.this, "编辑成功", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 2000);
        // onCreate(null);//调用此方法刷新页面

    }

    private void RXDfun() {

        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String FileUrlName = bundle.getString("FileUrlName");

        //得到的是链接和名字
        //读取文件dao ------  textview
        Fromfileurl.setText(FileUrlName);
        //读取文件dao ------  editview
        Ev_file.setText(FileoOperations.readFromSDFile(this, FileUrlName));


    }


}
